/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.sqloutput;

import java.awt.Color;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.*;
import org.openide.util.Exceptions;
import org.prashant.sqlduniya.sqloutput.util.DataExporter;

/**
 *
 * @author prashant
 */
public class TableViewPanel extends javax.swing.JPanel {

    public JXTable getjXTableData() {
        return jXTableData;
    }

    /**
     * Creates new form TableView
     */
    public TableViewPanel() {
        initComponents();
        Highlighter simpleStriping = HighlighterFactory.createSimpleStriping();
        PatternPredicate patternPredicate = new PatternPredicate("ˆM", 1);
        ColorHighlighter magenta = new ColorHighlighter(patternPredicate, null,
                Color.MAGENTA, null, Color.MAGENTA);
        Highlighter shading = new ShadingColorHighlighter(
                new HighlightPredicate.ColumnHighlightPredicate(1));

        jXTableData.setHighlighters(simpleStriping, magenta, shading);
        jXTableData.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW,
                Color.cyan, null));
        jXTableData.setRolloverEnabled(true);
        jXTableData.setColumnControlVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnExportXLS = new javax.swing.JButton();
        btnExportPDF = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTableData = new org.jdesktop.swingx.JXTable();

        setLayout(new java.awt.BorderLayout());

        jToolBar1.setRollover(true);

        btnExportXLS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/prashant/sqlduniya/sqloutput/Excel-icon16.png"))); // NOI18N
        btnExportXLS.setText(org.openide.util.NbBundle.getMessage(TableViewPanel.class, "TableViewPanel.btnExportXLS.text")); // NOI18N
        btnExportXLS.setFocusable(false);
        btnExportXLS.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExportXLS.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnExportXLS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportXLSActionPerformed(evt);
            }
        });
        jToolBar1.add(btnExportXLS);

        btnExportPDF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/prashant/sqlduniya/sqloutput/PDF-icon16.png"))); // NOI18N
        btnExportPDF.setText(org.openide.util.NbBundle.getMessage(TableViewPanel.class, "TableViewPanel.btnExportPDF.text")); // NOI18N
        btnExportPDF.setFocusable(false);
        btnExportPDF.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExportPDF.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnExportPDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportPDFActionPerformed(evt);
            }
        });
        jToolBar1.add(btnExportPDF);

        add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jXTableData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jXTableData.setHorizontalScrollEnabled(true);
        jScrollPane1.setViewportView(jXTableData);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnExportXLSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportXLSActionPerformed
        File file = new File("/home/prashant/test.xls");
        DataExporter.fillData("test", jXTableData, file);
        Desktop desktop = null;
        // Before more Desktop API is used, first check 
        // whether the API is supported by this particular 
        // virtual machine (VM) on this particular host.
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.browse(file.toURI());
            } catch (IOException ex) {
                
            }
        }
    }//GEN-LAST:event_btnExportXLSActionPerformed

    private void btnExportPDFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportPDFActionPerformed
        File file = new File("/home/prashant/test.pdf");
        DataExporter.fillPage(jXTableData,file);
        
        Desktop desktop = null;
        // Before more Desktop API is used, first check 
        // whether the API is supported by this particular 
        // virtual machine (VM) on this particular host.
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.browse(file.toURI());
            } catch (IOException ex) {
                
            }
        }
    }//GEN-LAST:event_btnExportPDFActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExportPDF;
    private javax.swing.JButton btnExportXLS;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private org.jdesktop.swingx.JXTable jXTableData;
    // End of variables declaration//GEN-END:variables
}
